import csv
import polib
import os,sys
import configparser

config = configparser.ConfigParser()
config.read('settings.ini')

en_file = config.get('Paths', 'en_file')
ru_file = config.get('Paths', 'ru_file')
zh_file = config.get('Paths', 'zh_file')
search_path = config.get('Paths','search_path')

def create_ru(po_file_path):
    if not os.path.isfile(po_file_path):
        po = polib.POFile()
        # 添加头部元数据
        po.metadata = {
            'Project-Id-Version': '1.0',
            'Report-Msgid-Bugs-To': '8794012@gmail.com',
            'POT-Creation-Date': '2023-05-11 12:00+0100',
            'PO-Revision-Date': '2023-05-11 12:00+0100',
            'Last-Translator': 'zhou jian <8794012@gmail.com>',
            'Language-Team': 'English <8794012@gmail.com>',
            'Language': 'ru',
            'MIME-Version': '1.0',
            'Content-Type': 'text/plain; charset=UTF-8',
            'Content-Transfer-Encoding': '8bit',
            'Plural-Forms': 'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);'
        }
        po.save(po_file_path)
def create_en(po_file_path):
    if not os.path.isfile(po_file_path):
        po = polib.POFile()
        # 添加头部元数据
        po.metadata = {
            'Project-Id-Version': '1.0',
            'Report-Msgid-Bugs-To': '8794012@gmail.com',
            'POT-Creation-Date': '2023-05-11 12:00+0100',
            'PO-Revision-Date': '2023-05-11 12:00+0100',
            'Last-Translator': 'zhou jian <8794012@gmail.com>',
            'Language-Team': 'Ru <8794012@gmail.com>',
            'Language': 'en',
            'MIME-Version': '1.0',
            'Content-Type': 'text/plain; charset=UTF-8',
            'Content-Transfer-Encoding': '8bit',
            'Plural-Forms': 'nplurals=2; plural=(n != 1);'
        }
        po.save(po_file_path)
def create_zh(po_file_path):
    if not os.path.isfile(po_file_path):
        po = polib.POFile()
        # 添加头部元数据
        po.metadata = {
            'Project-Id-Version': '1.0',
            'Report-Msgid-Bugs-To': '8794012@gmail.com',
            'POT-Creation-Date': '2023-05-11 12:00+0100',
            'PO-Revision-Date': '2023-05-11 12:00+0100',
            'Last-Translator': 'zhou jian <8794012@gmail.com>',
            'Language-Team': 'Zh <8794012@gmail.com>',
            'Language': 'zh',
            'MIME-Version': '1.0',
            'Content-Type': 'text/plain; charset=UTF-8',
            'Content-Transfer-Encoding': '8bit',
            'Plural-Forms': 'nplurals=1; plural=0;'  # 中文通常没有复数形式
        }
        po.save(po_file_path)

def add_to_po(key,en,ru, po_file_path_en, po_file_path_ru,po_file_path_zh):
    # 检查并创建你的现有的PO文件
    if not os.path.isfile(po_file_path_en):
        create_en(po_file_path_en)
    if not os.path.isfile(po_file_path_ru):
        create_ru(po_file_path_ru)
    if not os.path.isfile(po_file_path_zh):
        create_ru(po_file_path_zh)
    # 打开你的现有的PO文件
    po_en = polib.pofile(po_file_path_en)
    po_ru = polib.pofile(po_file_path_ru)

    # 检查msgid是否已经存在
    for entry in po_en:
        if entry.msgid == key:
            print("msgid '{}' already exists in the file. Skipping...".format(key))
            return
    for entry in po_ru:
        if entry.msgid == key:
            print("msgid '{}' already exists in the file. Skipping...".format(key))
            return

    entry_en = polib.POEntry(
        msgid=key,
        msgstr=en
    )
    entry_ru = polib.POEntry(
        msgid=key,
        msgstr=ru
    )
    # 将 POEntry 添加到 PO 文件中
    po_en.append(entry_en)
    po_ru.append(entry_ru)
    
    # 保存PO文件
    po_en.save(po_file_path_en)
    po_ru.save(po_file_path_ru)


if __name__ == '__main__':
    # 检查命令行参数数量是否正确
    if len(sys.argv) != 4:
        print("请提供正确的参数数量。需要三个参数。")
    else:
        # 从命令行参数中获取传递的值
        key = sys.argv[1]
        en = sys.argv[2]
        ru = sys.argv[3]
        # 使用你的文件路径调用函数
        add_to_po(key,en,ru, en_file, ru_file,zh_file)